package com.inspur.edp.ai.flow.evaluator;

import com.inspur.edp.common.expr.evaluator.ExpressEvaluator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lizhaorui
 * @date 2025/10/10
 * @description
 */
public class DefaultStrCatEvaluator implements StrCatEvaluator {

    private String content;

    private List<Object> segments = new ArrayList<>();

    public void addStringSegment(String segment) {
        this.segments.add(segment);
    }

    public void addExpressSegment(ExpressEvaluator evaluator) {
        this.segments.add(evaluator);
    }

    public String evaluate(Map<String, Object> dataMap) {
        StringBuilder sb = new StringBuilder();
        for (Object segment : this.segments) {
            if (segment instanceof String) {
                sb.append((String) segment);
            } else {
                ExpressEvaluator evaluator = (ExpressEvaluator) segment;
                Object expressValue = evaluator.evaluate(dataMap);
                if(expressValue!=null){
                    sb.append(expressValue.toString());
                }
            }
        }
        return sb.toString();
    }

}